package com.supor.aiot.mine.ui.setting;

import android.annotation.SuppressLint;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.android.baseconfig.common.utils.Logc;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.app.api.UserImpl;
import com.supor.aiot.common.base.BaseViewModel;
import com.supor.aiot.common.entity.Result;

public class SettingViewModel extends BaseViewModel {

    private MutableLiveData<Result> logoutResult = new MutableLiveData<>();
    private MutableLiveData<Result> deleteResult = new MutableLiveData<>();

    public MutableLiveData<Result> getLogoutResult() {
        return logoutResult;
    }

    public MutableLiveData<Result> getDeleteResult() {
        return deleteResult;
    }

    public SettingViewModel(@NonNull Application application) {
        super(application);
    }

    @SuppressLint("CheckResult")
    public void logout() {
        UserImpl.getInstance().logout().subscribe(apiResult -> {
            if (apiResult.isSuccess()) {
                logoutResult.setValue(new Result.Success(Constants.Value.SUCCESS));
            } else {
                logoutResult.setValue(new Result.Error("logout failed"));
            }
        }, throwable -> {
            Logc.e("logout: " + throwable);
            logoutResult.setValue(new Result.Error(new Exception(throwable)));
        });
    }

    @SuppressLint("CheckResult")
    public void deleteUser(String bossUserId) {
        UserImpl.getInstance().deleteUser(bossUserId).subscribe(apiResult -> {
            if (apiResult.isSuccess()) {
                deleteResult.setValue(new Result.Success(Constants.Value.SUCCESS));
            } else {
                deleteResult.setValue(new Result.Error("delete user failed"));
            }
        }, throwable -> {
            Logc.e("deleteUser: " + throwable);
            deleteResult.setValue(new Result.Error(new Exception(throwable)));
        });
    }

}
